const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    obj: {
      input_showed: false,
      input_search_value: "",
      data: [],
      noData: false
    },
    loadMore: true,
    refresh: true,
    loadMoreTitle: "正在加载...",
    noData: false,
    totalPage: 0,
    height: 0,
    touchMove: 0,
    touchStart: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      theme: app.globalData.theme
    });
    wx.getSystemInfo({
      success: res=>{
        this.setData({
          height: res.windowHeight - 44
        });
      }
    });
    this.refresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 显示搜索
   */
  showInput: function() {
    this.setData({
      ["obj.input_showed"]: true
    });
  },
  /**
   * 搜索
   */
  onBindInput: function() {
    this.setData({
      ["obj.input_showed"]: true
    });
    if (this.data.obj["input_search_value"].length != 0) {
      this.getArea();
    }
  },
  /**
   * 清除输入内容
   */
  clearInput: function() {
    this.setData({
      ["obj.input_search_value"]: "",
      ["obj.input_showed"]: false,
      ["obj.data"]: [],
    });
    this.refresh();
  },
  /**
   * 获取输入框的值
   */
  inputTyping: function(e) {
    this.setData({
      ["obj.input_search_value"]: e.detail.value
    });
  },
  inputConfirm: function() {
    if (this.data.obj["input_search_value"].length != 0) {
      this.getArea();
    }
  },
  getArea: function() {
    wx.showLoading({
      title: '正在搜索...',
    })
    app.globalData.pageNumber = 1;
    app.wxGetRequest("/shopping/getProductList", {
      name: this.data.obj["input_search_value"],
      pageNumber: 1,
      pageSize: 10
    }).then(result => {
      wx.hideLoading();
      if (result.data.code == 1) {
        this.setData({
          ["obj.data"]: [],
          ["obj.data"]: result.data.data.list,
          ["obj.noData"]: false
        });
      } else {
        this.setData({
          ["obj.data"]: [],
          ["obj.noData"]: true
        });
        wx.showToast({
          title: result.data.msg,
        })

      }
    });
  },
  /**
   * 选中列表某一项
   */
  onListTap: function(e) {
    app.globalData.area_name = e.currentTarget.dataset.areaname;
    wx.navigateBack({
      delta: getCurrentPages()
    })
  },
  //详情
  onMallDetail: function(e) {
    wx.navigateTo({
      url: "/pages/shop/shop_detail/shop_detail?id=" + e.currentTarget.dataset.id,
    })
  },
  onTouchStart: function(e) {
    this.setData({
      touchStart: e.touches[0].pageY
    });
  },
  /**
   * 自定义刷新
   */
  onMove: function(e) {
    if (e.target.offsetTop <= this.data.height && (e.touches[0].pageY - this.data.touchStart) > 0 && this.data.refresh) {
      app.globalData.pageNumber = 1;
      this.setData({
        refresh: false,
      });
      this.refresh();
    }

  },
  refresh: function() {
    app.globalData.pageNumber = 1;
    app.wxGetRequest("/shopping/getProductList", {
      name: this.data.obj["input_search_value"] ? this.data.obj["input_search_value"] : "",
      pageNumber: 1,
      pageSize: 6
    }).then(result => {
      let data = result.data.data;
      if (result.data.code == 1) {
        if (data.list.length != 0) {
          this.setData({
            ["obj.data"]: data.list,
            noData: false,
            totalPage: data.totalPage,
          });
        } else {
          this.setData({
            ["obj.data"]: data.list,
            noData: true,
            totalPage: data.totalPage,
          });
        }
      } else {
        // wx.showToast({
        //   title: res.data.msg,
        // })
      }
      setTimeout(()=> {
        this.setData({
          refresh: true
        })
      }, 1000);
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  bindscrolltolower: function() {
    if (app.globalData.pageNumber < this.data.totalPage) {
      this.setData({
        loadMore: false,
      });
      app.globalData.pageNumber++;
      app.wxGetRequest("/shopping/getProductList", {
        name: this.data.obj["input_search_value"] ? this.data.obj["input_search_value"] : "",
        pageNumber: app.globalData.pageNumber,
        pageSize: 10
      }).then(result => {
        let data = result.data.data;
        if (result.data.code == 1) {
          this.setData({
            loadMoreTitle: "正在加载...",
            totalPage: data.totalPage,
            ["obj.data"]: this.data.obj["data"].concat(data.list)
          });
          setTimeout(()=> {
            this.setData({
              loadMore: true
            });
          }, 2000);
        } else {
          wx.showToast({
            title: result.data.msg,
          });
        }
      });
    } else {
      this.setData({
        loadMore: false,
        loadMoreTitle: "暂无数据了"
      });
      setTimeout(function() {
        this.setData({
          loadMore: true,
        });
      }, 2000);
    }
  },
})